package com.henghai.zhenxuan.ui.page.mine;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.bean.ProductOrderListBean;
import com.henghai.zhenxuan.domain.request.ProductOrderRequester;
import com.henghai.zhenxuan.ui.page.adapter.IntegralOrderListAdapter;
import com.google.android.material.tabs.TabLayout;
import com.henghai.zhenxuan.R;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

public class IntegralOrderFragment extends BaseFragment {

    private IntegralOrderStates mStates;
    private IntegralOrderListAdapter mAdapter;
    private ProductOrderRequester mProductOrderRequester;
    private int current = 0;
    private RefreshLayout mRefreshLayout;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(IntegralOrderStates.class);
        mProductOrderRequester = getFragmentScopeViewModel(ProductOrderRequester.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mProductOrderRequester.getResult().observe(this, result -> {
            if (mRefreshLayout != null) {
                mRefreshLayout.finishRefresh();
            }
            if (!result.getResponseStatus().isSuccess()) {
                TipDialog.show(result.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.list.set(result.getResult().getData().getRows());
        });
        mProductOrderRequester.request(getViewLifecycleOwner(), current + "");
    }

    public class TabListenerHandler implements TabLayout.OnTabSelectedListener {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            current = tab.getPosition();
            mProductOrderRequester.request(getViewLifecycleOwner(), current + "");
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    }

    public class ListenerRefreshHandler implements OnRefreshListener {
        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mRefreshLayout = refreshLayout;
            mProductOrderRequester.request(getViewLifecycleOwner(), current + "");
        }
    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new IntegralOrderListAdapter(getContext());
        mAdapter.setOnItemClickListener((i, productBean, i1) -> {
            Bundle bundle = new Bundle();

            bundle.putInt(OrderInfoFragment.ORDER_KEY, productBean.getOrderId());
            nav().navigate(R.id.action_integralOrder_to_order_info, bundle);
        });

        return new DataBindingConfig(com.henghai.zhenxuan.R.layout.fragment_integral_order, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter).addBindingParam(BR.listener, new ListenerHandler())
                .addBindingParam(BR.listenerRefresh, new ListenerRefreshHandler())
                .addBindingParam(BR.listenerTab, new TabListenerHandler());

    }

    public static class IntegralOrderStates extends StateHolder {
        public final State<ArrayList<ProductOrderListBean.RowsBean>> list = new State<>(new ArrayList<>());
    }
}
